package com.ruoyi.web.controller.shop.goods;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.goods.domain.ShopAttr;
import com.ruoyi.shop.goods.service.IShopAttrService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品属性Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */

@Controller
@RequestMapping("/shop/attr")
public class ShopAttrController extends BaseController {
	private String prefix = "shop/goods/attr";

	@Autowired
	private IShopAttrService shopAttrService;

	@RequiresPermissions("shop:attr:view")
	@GetMapping()
	public String attribute() {
		return prefix + "/attr";
	}

	/**
	 * 查询商品属性列表
	 */
	@RequiresPermissions("shop:attr:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopAttr shopAttr) {
		startPage();
		List<ShopAttr> list = shopAttrService.selectAttrList(shopAttr);
		return getDataTable(list);
	}

	/**
	 * 新增商品属性
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存商品属性
	 */
	@RequiresPermissions("shop:attr:add")
	@Log(title = "商品属性", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopAttr shopAttr) {
		return toAjax(shopAttrService.insertAttr(shopAttr));
	}

	/**
	 * 选择商品规格
	 * 
	 * @return
	 */
	@GetMapping("/selectSpec")
	public String selectSpec() {
		return prefix + "/selectSpec";
	}

	/**
	 * 修改商品规格
	 */
	@RequiresPermissions("shop:attr:edit")
	@GetMapping("/editSpec/{attrId}")
	public String editSpec(@PathVariable("attrId") Long attrId, ModelMap mmap) {
		ShopAttr shopAttr = shopAttrService.selectAttrById(attrId);
		mmap.put("shopAttr", shopAttr);
		return prefix + "/editSpec";
	}

	/**
	 * 选择商品参数
	 * 
	 * @return
	 */
	@GetMapping("/selectParam")
	public String selectParam() {
		return prefix + "/selectParam";
	}

	/**
	 * 修改商品参数
	 */
	@RequiresPermissions("shop:attr:edit")
	@GetMapping("/editParam/{attrId}")
	public String editParam(@PathVariable("attrId") Long attrId, ModelMap mmap) {
		ShopAttr shopAttr = shopAttrService.selectAttrById(attrId);
		mmap.put("shopAttr", shopAttr);
		return prefix + "/editParam";
	}

	/**
	 * 修改保存商品属性
	 */
	@RequiresPermissions("shop:attr:edit")
	@Log(title = "商品属性", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopAttr shopAttr) {
		return toAjax(shopAttrService.updateAttr(shopAttr));
	}

	/**
	 * 删除商品属性
	 */
	@RequiresPermissions("shop:attr:remove")
	@Log(title = "商品属性", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(shopAttrService.deleteAttrByIds(ids));
	}

}
